package com.thinkersre.controller;


import com.thinkersre.annotation.LoginRequired;
import com.thinkersre.entity.Event;
import com.thinkersre.entity.Page;
import com.thinkersre.entity.User;
import com.thinkersre.event.EventProducer;
import com.thinkersre.service.FollowService;
import com.thinkersre.service.UserService;
import com.thinkersre.util.HostHolder;
import com.thinkersre.util.ThinkersReConstant;
import com.thinkersre.util.ThinkersReUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
public class FollowController implements ThinkersReConstant {
    @Resource
    private FollowService followService;
    @Resource
    private HostHolder hostHolder;
    @Resource
    private UserService userService;
    @Resource
    private EventProducer eventProducer;
    @PostMapping("/follow")
    @ResponseBody
    @LoginRequired
    public String follow(int entityType,int entityId){
        User user = hostHolder.getUser();
        followService.follow(user.getId(), entityType,entityId);

        //触发关注事件
        Event event = new Event().setTopic(TOPIC_FOLLOW)
                .setUserId(hostHolder.getUser().getId())
                .setEntityType(entityType)
                .setEntityId(entityId)
                .setEntityUserId(entityId); //被关注实体的实体id同时也是该实体的作者id，是他自己
        eventProducer.fireEvent(event);

        return ThinkersReUtil.getJSONString(0,"已关注");
    }


    @LoginRequired
    @PostMapping("/unfollow")
    @ResponseBody
    public String unfollow(int entityType,int entityId){
        User user = hostHolder.getUser();
        followService.unfollow(user.getId(), entityType,entityId);

        return ThinkersReUtil.getJSONString(0,"已取消关注");
    }

    @GetMapping("/followees/{userId}")
    public String getFollowees(@PathVariable("userId") int userId, Page page, Model model){
        User user = userService.findUserById(userId);
        if(user == null){
            throw new RuntimeException("该用户不存在");
        }
        model.addAttribute("user",user);
        page.setPageSize(5);
        page.setPath("/followees/"+userId);
        page.setRowsTotal((int) followService.findFolloweeCount(userId,ENTITY_TYPE_USER));
        List<Map<String,Object>> userList = followService.findFollowees(userId,page.getOffset(),page.getPageSize());
        if (userList!=null){
            for (Map<String,Object> map:userList){
                User u = (User) map.get("user");
                map.put("hasFollowed",hasFollowed(u.getId()));
            }
        }
        model.addAttribute("users",userList);

        return "/site/followee";

    }



    @GetMapping("/followers/{userId}")
    public String getFollowers(@PathVariable("userId") int userId, Page page, Model model){
        User user = userService.findUserById(userId);
        if(user == null){
            throw new RuntimeException("该用户不存在");
        }
        model.addAttribute("user",user);
        page.setPageSize(5);
        page.setPath("/followers/"+userId);
        page.setRowsTotal((int) followService.findFollowerCount(ENTITY_TYPE_USER,userId ));
        List<Map<String,Object>> userList = followService.findFollowers(userId,page.getOffset(),page.getPageSize());
        if (userList!=null){
            for (Map<String,Object> map:userList){
                User u = (User) map.get("user");
                map.put("hasFollowed",hasFollowed(u.getId()));
            }
        }
        model.addAttribute("users",userList);

        return "/site/follower";

    }




    private boolean hasFollowed(int userId){
        if (hostHolder.getUser() == null){
            return false;
        }

        return followService.hasFollowed(hostHolder.getUser().getId(),ENTITY_TYPE_USER,userId);

    }


}
